package com.xj.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class MySqlConnector {

    public static Connection getConnection(String url, String user, String password) {
        Connection connection = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            System.out.println("获取数据库连接失败: " + e.getMessage());
            e.printStackTrace();
        }
        return connection;
    }
    public static Connection getConnection(String type) {
        String url1 = null,url2 = null,user1 = null,user2 = null,password1 = null,password2 = null;
        url1 = PropertiesConfig.prop.getProperty("source.url");
        url2 = PropertiesConfig.prop.getProperty("target.url");
        user1 = PropertiesConfig.prop.getProperty("source.user");
        user2 = PropertiesConfig.prop.getProperty("target.user");
        password1 = PropertiesConfig.prop.getProperty("source.password");
        password2 = PropertiesConfig.prop.getProperty("target.password");
        if(type.equals("source")) {
            return getConnection(url1, user1, password1);
        } else if(type.equals("target")) {
            return getConnection(url2, user2, password2);
        }
        return null;
    }
}
